/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2013
 */

package com.gxuwz.byssxsys.action;

import static javacommon.util.extjs.Struts2JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.extjs.ExtJsPageHelper;
import javacommon.util.extjs.ListRange;
import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;

import com.gxuwz.byssxsys.model.Admin;
import com.gxuwz.byssxsys.service.AdminManager;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */
public class AdminAction extends BaseStruts2Action implements Preparable,
		ModelDriven<Object> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = "id desc";

	// forward paths
	protected static final String QUERY_JSP = "/pages/Admin/query.jsp";
	protected static final String LIST_JSP = "/pages/Admin/list.jsp";
	protected static final String CREATE_JSP = "/pages/Admin/create.jsp";
	protected static final String EDIT_JSP = "/pages/Admin/edit.jsp";
	protected static final String SHOW_JSP = "/pages/Admin/show.jsp";
	// redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/Admin/list.do";

	private AdminManager adminManager;

	private Admin admin;
	java.lang.Integer id = null;
	//private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			admin = new Admin();
		} else {
			admin = adminManager.getById(id);
		}
	}

	/** 通过spring自动注入 */
	public void setAdminManager(AdminManager manager) {
		this.adminManager = manager;
	}

	public Object getModel() {
		return admin;
	}

	public void setId(java.lang.Integer val) {
		this.id = val;
	}

//	public void setItems(String[] items) {
//		this.items = items;
//	}

	/**
	 * ExtGrid使用 列表
	 * 
	 * @throws IOException
	 */
	public void extlist() throws IOException {
		@SuppressWarnings("rawtypes")
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(
				getRequest(), DEFAULT_SORT_COLUMNS);
		
		Page<?> page = adminManager.findPage(pr);
		@SuppressWarnings("unchecked")
		List<Admin> Adminlist = (List<Admin>) page.getResult();
		ListRange<Admin> resultList = new ListRange<Admin>();
		resultList.setList(Adminlist);
		resultList.setTotalSize(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(resultList);
	}

	/**
	 * extGrid保存
	 * 
	 * @throws IOException
	 */
	public void extsave() throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			adminManager.save(admin);
			result.put("success", true);
			result.put("msg", "添 加 成 功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}

	/**
	 * extGrid修改
	 * 
	 * @throws IOException
	 */
	public void extupdate() throws IOException {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			adminManager.update(admin);
			result.put("success", true);
			result.put("msg", "修 改 成 功!");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}

	/**
	 * extGrid删除
	 * 
	 * @throws IOException
	 */
	public void extdelete() throws IOException {
		String ids = getRequest().getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			for (int i = 0; i < idarray.length; i++) {
				java.lang.Integer id = new java.lang.Integer(
						(String) idarray[i]);
				adminManager.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(result);
	}

}
